package com.shycloud.mido.member.service.impl;

import java.util.Date;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.entity.Member;
import com.shycloud.mido.member.api.entity.SmMemberGoldLog;
import com.shycloud.mido.member.api.entity.SmPrintDetailed;
import com.shycloud.mido.member.api.entity.SmPrintPrice;
import com.shycloud.mido.member.mapper.MemberMapper;
import com.shycloud.mido.member.mapper.SmMemberGoldLogMapper;
import com.shycloud.mido.member.mapper.SmPrintDetailedMapper;
import com.shycloud.mido.member.mapper.SmPrintPriceMapper;
import com.shycloud.mido.member.service.SmPrintPriceService;

import lombok.AllArgsConstructor;

@Service
@AllArgsConstructor
public class SmPrintPriceServiceImpl extends ServiceImpl<SmPrintPriceMapper, SmPrintPrice> implements SmPrintPriceService {


	/** 打印模块 mapper */
	private SmPrintPriceMapper printPriceMapper;
	/** 用户模块 mapper */
	private MemberMapper memberMapper;
	/** 充值记录模块 mapper */
	private SmMemberGoldLogMapper memberGoldLogMapper;
	/** 打印详情 mapper */
	private SmPrintDetailedMapper printDetailedMapper;

	/** 价格 = 0 */
	private static final Integer FREE_PRICE = 0;

	/**
	 * 打印曲谱
	 *
	 * @return Boolean
	 * @author nianhua.jiang
	 * @date 2020/8/19 10:01
	 **/
	@Override
	@Transactional(rollbackFor = Exception.class, timeout = 36000)
	public Boolean print(Integer musicId, String musicName) {

		//获取打印价格
		Integer printPrice = printPriceMapper.selectOne(new QueryWrapper<>()).getPrice();
		//获取用户ID
		String memberId = SecurityUtils.getMember().getId();
		//根据用户ID获取用户信息
		Member member = memberMapper.selectById(memberId);
		//虚拟币消费记录id
		Integer goldLogId = 0;

		if (member == null) {
			throw new BusinessException("用户信息获取失败，请重新登录");
		}

		//判断是否还有赠送次数，有先抵扣赠送次数
		if (member.getPrintAmountGive() > 0) {
			//扣除免费使用次数
			if (printForFree(member)) {
				//记录信息,返回 logId
				goldLogId = insertMemberLog(member, musicName, FREE_PRICE);
			}
		}

		//如果没有赠送次数了，抵扣购买次数
		if (member.getPrintAmountBuy() > 0) {
			if (printForBuy(member)) {
				//记录信息,返回 logId
				goldLogId = insertMemberLog(member, musicName, FREE_PRICE);
			}
		}

		//如果免费/购买次数都没有了，则扣虚拟币余额
		if (member.getPrintAmountGive() <= 0 && member.getPrintAmountBuy() <= 0) {
			//购买次数和赠送次数都没有了，根据价格抵扣钻石
			if (member.getGold() < printPrice) {
				//FBI Warning 千万不要私自修改文案，前端有写死判断，如需修改请联系APP前端工程师
				throw new BusinessException("余额不足，您先充值");
			}
			//更新账户余额
			Integer gold = member.getGold() - printPrice;
			member.setGold(gold);
			memberMapper.updateById(member);
			//新增虚拟币消费记录
			goldLogId = insertMemberLog(member, musicName, printPrice);
		}
		//新增打印记录详情，随后返回结果
		return insertPrintDetail(musicId, musicName, goldLogId);
	}

	/**
	 * 新增打印详情
	 *
	 * @param musicId
	 * @param musicName
	 * @param goldLogId
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/8/19 13:39
	 **/
	private Boolean insertPrintDetail(Integer musicId, String musicName, Integer goldLogId) {

		SmPrintDetailed printDetailed = new SmPrintDetailed();
		//创建时间
		printDetailed.setCreateTime(new Date());
		//金币log主键ID
		printDetailed.setGoldLogId(goldLogId);
		//曲目ID
		printDetailed.setPrintMusicId(musicId);
		//曲目名称
		printDetailed.setPrintMusicName(musicName);
		//新增
		return printDetailedMapper.insert(printDetailed) == 1;
	}

	/**
	 * 扣除购买打印次数【目前业务没有购买次数，扩展用】
	 *
	 * @param member
	 * @return Boolean
	 * @author nianhua.jiang
	 * @date 2020/8/19 13:45
	 **/
	private Boolean printForBuy(Member member) {

		//剩余次数 - 1
		int amount = member.getPrintAmountBuy() - 1;
		member.setPrintAmountBuy(amount);
		//使用次数 + 1
		int amountUse = member.getPrintAmountUsed() + 1;
		member.setPrintAmountUsed(amountUse);

		return memberMapper.updateById(member) == 1;
	}

	/**
	 * 扣除免费打印次数
	 *
	 * @param member
	 * @return Boolean
	 * @author nianhua.jiang
	 * @date 2020/8/19 13:45
	 **/
	private Boolean printForFree(Member member) {

		//剩余次数 - 1
		int amount = member.getPrintAmountGive() - 1;
		member.setPrintAmountGive(amount);
		//使用次数 + 1
		int amountUse = member.getPrintAmountUsed() + 1;
		member.setPrintAmountUsed(amountUse);

		return memberMapper.updateById(member) == 1;
	}

	/**
	 * 新增虚拟币消费记录
	 *
	 * @param member
	 * @param musicName
	 * @author nianhua.jiang
	 * @date 2020/8/19 13:01
	 **/
	private Integer insertMemberLog(Member member, String musicName, Integer gold) {

		//新增虚拟币消费记录
		SmMemberGoldLog smMemberGoldLog = new SmMemberGoldLog();
		//价格
		smMemberGoldLog.setGold(gold);
		//用户ID
		smMemberGoldLog.setMemberId(member.getId());
		//类型 消费
		smMemberGoldLog.setType("0");
		//创建时间
		smMemberGoldLog.setCreateTime(new Date());
		//备注信息
		smMemberGoldLog.setComment("打印曲谱-> " + musicName);

		memberGoldLogMapper.insert(smMemberGoldLog);

		return smMemberGoldLog.getId();
	}


}
